<?php defined('BASEPATH') OR exit('No direct script access allowed');

// Code here is run before admin controllers
class Admin_Controller extends MY_Controller
{
	protected $data = array();
	
	function Admin_Controller()
	{
		parent::__construct();

	    $this->load->library('ion_auth');
	    $this->lang->load('global');
	    $this->lang->load('users/user');
	    $this->lang->load('layout/buttons');
	    $this->lang->load('layout/admin_layout');
	    $this->load->model('users/users_model', 'users_m');
	    $this->load->model('users/permission_model', 'permission_m');
	    $this->load->model('groups/group_model', 'group_m');
	    $this->load->helper('user');
	    $this->load->helper('array');
	    $this->load->helper('ckeditor');
	    $this->load->library('form_validation');
	    $this->load->config('modules');
	    
	    $this->user = $this->template->current_user = ci()->current_user = $this->current_user = $this->ion_auth->get_user();
	    ci()->permissions = $this->permissions = $this->current_user ? $this->permission_m->get_group($this->current_user->group_id) : array();
	    $this->shortcuts = array(
	    		array('name' => 'Dashboard', 	'link' => site_url('admin')), 
	    		array('name' => 'Sales', 		'link' => site_url('admin/sales')), 
	    		array('name' => 'Products', 	'link' => site_url('admin/catalog/products'))
	    ); 

	    ci()->module = $this->module = $this->uri->segment(2);
	    ci()->action = $this->action = $this->uri->segment(3);
//	          检查管理员权限
	    if( ! self::_check_access() )
	    {
			redirect('admin/login');
	    }
//	   	 设置模板路径文件夹为views/admin
	    $this->load->library('Template', array('folder_admin' => TRUE));
	    $this->template->set_data(array(
	    		'module' =>$this->uri->segment(2), 
	    		'action' =>$this->uri->segment(3), 
	    		'shortcuts' => $this->shortcuts,
	    		'logined_user' => $this->user,
	    		'supported_languages' => $this->config->item('supported_languages')
	    ));
	}
	
	private function _check_access()
	{
		// These pages get past permission checks
		$ignored_pages = array('admin/login', 'admin/logout');

		// Check if the current page is to be ignored
		$current_page = $this->uri->segment(1, '') . '/' . $this->uri->segment(2, 'index');

		// Dont need to log in, this is an open page
		if (in_array($current_page, $ignored_pages))
		{
			return TRUE;
		}
		else if ( ! $this->current_user)
		{
			redirect('admin/login');
		}

		// Admins can go straight in
		else if ($this->current_user->group === 'admin')
		{
			return TRUE;
		}

		// Well they at least better have permissions!
		else if ($this->current_user)
		{
			// We are looking at the index page. Show it if they have ANY admin access at all
			if ($current_page == 'admin/index' && $this->permissions)
			{
				return TRUE;
			}
			else
			{
				if(empty($this->module)){
					$this->module = "";
				}
// 				p($this->permissions);
				// Check if the current user can view that page
				if(array_key_exists($this->module, $this->permissions)){
					$modules = $this->config->item('modules');
					foreach($modules as $module){
						foreach($module['roles'] as $role){
							if($this->action === $role && $this->module === $module['slug'] && !isset($this->permissions[$this->module]->{$role})){
								return FALSE;
							}
						}
					}
					return TRUE;
				}
			}
		}

		return FALSE;
	}
}